home *** CD-ROM | disk | FTP | other *** search
/ Internet Pratica / IPRAT_01.iso / ASP / ASPapp Portal / admin_cats.asp < prev    next >
Encoding:
Text File  |  2002-03-12  |  12.1 KB  |  440 lines

  1. <!-- #include file="i_utils.asp" -->
  2. <%
  3.  
  4. check_security(1) 
  5.  
  6. %>
  7. <%
  8.     dim Cat
  9.     dim CatID
  10.     dim CatTypeId
  11.     dim Description
  12.     dim ParentId
  13.     dim RelatedId
  14.     dim edit_cats_sql
  15.     dim Parent
  16.     dim CatType
  17.     dim view_cats_sql
  18.  
  19. sub request_edit_cats
  20. ''' request expected input parameters for this form
  21.     Cat = request("Cat")
  22.     CatID = request("CatID")
  23.     CatTypeId = request("CatTypeId")
  24.     Description = request("Description")
  25.     ParentId = request("ParentId")
  26.     RelatedId = request("RelatedId")
  27. end sub
  28. sub request_view_cats
  29. ''' request expected input parameters for this form
  30.     Parent = request("Parent")
  31.     ParentId = request("ParentId")
  32.     CatID = request("CatID")
  33.     Cat = request("Cat")
  34.     RelatedId = request("RelatedId")
  35.     CatType = request("CatType")
  36. end sub
  37.  
  38. sub validate_edit_cats
  39. ''' request and validate data entered from this form
  40.     Cat = trim(request("Cat"))
  41.     if Cat = "" then
  42.         error_list.add "484304","Category must be specified."
  43.         b_error = true
  44.     end if
  45.     CatID = trim(request("CatID"))
  46.     CatTypeId = trim(request("CatTypeId"))
  47.     Description = trim(request("Description"))
  48.     ParentId = trim(request("ParentId"))
  49.     RelatedId = trim(request("RelatedId"))
  50. end sub
  51. sub validate_view_cats
  52. ''' request and validate data entered from this form
  53.     Parent = trim(request("Parent"))
  54.     ParentId = trim(request("ParentId"))
  55.     CatID = trim(request("CatID"))
  56.     Cat = trim(request("Cat"))
  57.     RelatedId = trim(request("RelatedId"))
  58.     CatType = trim(request("CatType"))
  59. end sub
  60.  
  61.  
  62. sub db_select_edit_Cats
  63.     sql = "SELECT " & _ 
  64.     "Cat, " & _ 
  65.     "CatID, " & _ 
  66.     "CatTypeId, " & _ 
  67.     "Description, " & _ 
  68.     "ParentId, " & _ 
  69.     "RelatedId FROM Cats" & _ 
  70.     " WHERE " & _ 
  71.     "Cats.CatID = " & to_sql(CatID,"number") & ""
  72.     on error resume next
  73.     set rs = cn.Execute(sql)
  74.     if err.number <> 0 then
  75.         b_error = true
  76.         error_list.add "select_data_edit_Cats", "The data selection failed. " & err.description
  77.     elseif rs.EOF then
  78.         b_results = false
  79.         msg_list.add "select_data_edit_Cats", "The record was removed from the database."
  80.     else
  81.         Cat = rs("Cat")
  82.         CatID = rs("CatID")
  83.         CatTypeId = rs("CatTypeId")
  84.         Description = rs("Description")
  85.         ParentId = rs("ParentId")
  86.         RelatedId = rs("RelatedId")
  87.     end if
  88.     rs.Close
  89.     on error goto 0
  90. end sub
  91. sub db_insert_edit_Cats
  92.     sql = "INSERT INTO Cats" & _ 
  93.     "(" & _ 
  94.     "Cat," & _ 
  95.     "CatTypeId," & _ 
  96.     "Description," & _ 
  97.     "ParentId," & _ 
  98.     "RelatedId" & _ 
  99.     ") VALUES (" & to_sql(Cat,"text") & "," & _ 
  100.     "" & to_sql(CatTypeId,"number") & "," & _ 
  101.     "" & to_sql(Description,"text") & "," & _ 
  102.     "" & to_sql(ParentId,"number") & "," & _ 
  103.     "" & to_sql(RelatedId,"number") & ")" & _ 
  104.     ""
  105.     'response.write sql
  106.     on error resume next
  107.     cn.Execute(sql)
  108.     if err.Number <> 0 then
  109.         b_error = true
  110.         error_list.add "db_insert_edit_Cats" & err.Number ,"The database insert failed. " & err.Description
  111.     else
  112.         set rs = cn.Execute("SELECT @@IDENTITY")
  113.         CatID = rs(0)
  114.         rs.Close
  115.         msg_list.add "db_insert_edit_Cats","The database insert was successful."    end if
  116.     on error goto 0
  117. end sub
  118. sub db_update_edit_Cats
  119.     sql = "UPDATE Cats SET " & _ 
  120.     "Cat = " & to_sql(Cat,"text") & ", " & _ 
  121.     "CatTypeId = " & to_sql(CatTypeId,"number") & ", " & _ 
  122.     "Description = " & to_sql(Description,"text") & ", " & _ 
  123.     "ParentId = " & to_sql(ParentId,"number") & ", " & _ 
  124.     "RelatedId = " & to_sql(RelatedId,"number") & " WHERE " & _ 
  125.     "CatID = " & to_sql(CatID,"number") & ""
  126.     'response.write sql
  127.     on error resume next
  128.     cn.execute(sql)
  129.     if err.number <> 0 then
  130.         b_error = true
  131.         error_list.add "db_update_edit_Cats" & err.Number ,"The database update failed. " & err.Description
  132.     else
  133.         msg_list.add "db_update_edit_Cats" & CatID,"The database update was successful."
  134.     end if
  135.     on error goto 0
  136. end sub
  137. sub db_delete_edit_Cats
  138.     sql = "DELETE FROM Cats" & _ 
  139.     " WHERE " & _ 
  140.     "CatID = " & to_sql(CatID,"number") & ""
  141.     'response.write sql
  142.     on error resume next
  143.     cn.Execute(sql)
  144.     if err.number <> 0 then
  145.         b_error = true
  146.         error_list.add "db_delete_edit_Cats" & err.Number ,"The database deletion failed. " & err.Description
  147.     else
  148.         msg_list.add "db_delete_edit_Cats","The record was removed."
  149.     end if
  150.     on error goto 0
  151. end sub
  152. sub db_select_view_Cats
  153.     view_Cats_sql = "SELECT " & _ 
  154.     "Cats2.Cat As Parent," & _ 
  155.     "Cats.Cat, " & _ 
  156.     "Cats.CatID, " & _ 
  157.     "CatType, " & _ 
  158.     "CatTypes.CatTypeId, " & _ 
  159.     "Cats.ParentId, " & _ 
  160.     "Cats.RelatedId As RelatedId FROM ((CatTypes LEFT JOIN Cats ON Cats.CatTypeId=CatTypes.CatTypeId) LEFT JOIN Cats As Cats2 ON Cats2.CatId = Cats.ParentId) ORDER BY Cats2.Cat"
  161.     if request("sortby") <> "" AND inStr(lcase(view_Cats_sql),"order by") = 0 then view_Cats_sql = view_Cats_sql + " ORDER BY " & request("sortby")
  162. end sub
  163. sub db_delete_view_Cats
  164.     sql = "DELETE FROM " & _ 
  165.     ""
  166.     'response.write sql
  167.     on error resume next
  168.     cn.Execute(sql)
  169.     if err.number <> 0 then
  170.         b_error = true
  171.     end if
  172.     on error goto 0
  173. end sub
  174.  
  175. do_search = request("do_search")
  176. ''' request form keys
  177. CatID = request("CatID")
  178. ''' request action
  179. action = lcase(request("action"))
  180. ''' action case handler
  181. select case action
  182.  
  183. case "select_edit_cats"
  184. '  select the requested key record from database
  185. if CatID <> "" then
  186.     db_select_edit_Cats
  187. else
  188.     b_error = true
  189.     error_list.add "edit_edit_Cats", "Specify record to select."
  190. end if
  191.  
  192.  
  193. case "insert_edit_cats"
  194. '  request form data and insert a new record into database
  195.  
  196. validate_edit_Cats
  197. db_insert_edit_Cats
  198. response.redirect request.servervariables("script_name")
  199.  
  200.  
  201. case "update_edit_cats"
  202. '  request form data and update an existing database record
  203. validate_edit_cats
  204. if not b_error then
  205. if CatID <> "" then
  206.     db_update_edit_cats
  207. else
  208.     b_error = true
  209.     error_list.add "update_edit_cats", "Specify record to update."
  210. end if
  211. end if
  212.  
  213.  
  214. case "delete_edit_cats"
  215. '  delete the requested key database record
  216. if CatID <> "" then
  217.     db_delete_edit_cats
  218.     response.redirect request.servervariables("script_name") & "?msg=The+record+was+deleted."
  219. else
  220.     b_error = true
  221.     error_list.add "delete_edit_cats", "Specify record to delete."
  222. end if
  223.  
  224.  
  225. end select
  226.  
  227. '  no action was specified, so handle the default case(s)
  228. if CatID <> "" then
  229.     db_select_edit_cats
  230. end if
  231.  
  232. db_select_view_cats
  233.  
  234. %>
  235.  
  236. <!-- #include file='i_header.asp' -->
  237. <!-- #include file='i_menu.asp' -->
  238.  
  239. <!-- #include file="i_menu_admin.asp" -->
  240. <%
  241. display_errs
  242. display_msg
  243. %>
  244.  
  245. <table class='headerTable'>
  246. <tr>
  247. <td class='headerTD'>
  248. <A href='admin_cats.asp'>Category Manager</A>
  249. </td>
  250. </tr>
  251. </table>
  252. <table  >
  253. <form name="edit_Cats" action="admin_cats.asp" method="get" >
  254. <tr>
  255.     <td class='labelTD'>Category</td>
  256.     <td class='dataTD' >
  257.         <input type=text  name="Cat" size="" maxlength="50" value="<% =Cat%>">
  258.     </td>
  259. </tr>
  260.         
  261.         <input type=hidden name="CatID" value="<% =CatID %>">
  262.         
  263. <tr>
  264.     <td class='labelTD'>Category Type <A href="admin_cattypes.asp">?</A></td>
  265.     <td class='dataTD' >
  266.         
  267.         <select name="CatTypeId" >
  268.         <option></option>
  269.         <%
  270.          =get_options ("SELECT CatTypeId,CatType FROM CatTypes", CatTypeId) 
  271.         %>
  272.         </select>
  273.          <A href="admin_cattypes.asp">define types</A>..
  274.     </td>
  275. </tr>
  276. <tr>
  277.     <td class='labelTD'>Description</td>
  278.     <td class='dataTD' >
  279.         
  280.         <textarea  name="Description" rows='6' cols='50'><% =Description%></textarea>
  281.         
  282.     </td>
  283. </tr>
  284. <tr>
  285.     <td class='labelTD'>Parent</td>
  286.     <td class='dataTD' >
  287.         
  288.         <select name="ParentId" >
  289.         <option></option>
  290.         <%
  291.          =get_options ("SELECT CatId,Cat FROM Cats", ParentId) 
  292.         %>
  293.         </select>
  294.         
  295.     </td>
  296. </tr>
  297. <tr>
  298.     <td class='labelTD'>Related</td>
  299.     <td class='dataTD' >
  300.         
  301.         <select name="RelatedId" >
  302.         <option></option>
  303.         <%
  304.          =get_options ("SELECT Cats3.CatId, Cats.Cat, Cats1.Cat, Cats2.Cat, Cats3.Cat FROM (((Cats As Cats3 left JOIN Cats As Cats2 ON Cats3.ParentId = Cats2.CatId) left JOIN Cats As Cats1 ON Cats2.ParentId = Cats1.CatId) LEFT JOIN Cats As Cats ON Cats1.ParentId = Cats.CatId) WHERE Cats.ParentId is NULL order by (Cats.Cat + Cats1.Cat + Cats2.Cat + Cats3.Cat)", RelatedId) 
  305.         %>
  306.         </select>
  307.         
  308.     </td>
  309. </tr>
  310. <tr>
  311.     <td class=labelTD align=right> 
  312.     <% if CatID <> "" then %><input type=button name=new_button value=new onclick="window.location = '<% =request.servervariables("script_name") %>'"><% end if %>
  313.     </td>
  314.     <td class=dataTD>
  315.     <% if CatID = "" then %><input type=submit name=insert_button value=insert><% end if %>
  316.     <% if CatID <> "" then %><input type=submit name=update_button value=update><% end if %>
  317.     <% if CatID <> "" then %><input type=submit name=delete_button value=delete onclick="document.edit_Cats.action.value = 'delete_edit_Cats'"><% end if %>
  318.     </td>
  319. <input type=hidden name="action" value="<% if CatID <> "" then %>update<% else %>insert<%end if %>_edit_Cats"></tr>
  320.  
  321. </form>
  322. </table>
  323.  
  324.  
  325. <%
  326.  
  327. page_no = request("page_no")
  328. if page_no = "" then page_no = 1
  329.  
  330. if view_Cats_sql <> "" then
  331.     cmd.CommandText = view_Cats_sql
  332.     rs.Filter = ""
  333.     rs.CursorLocation = 3
  334.     rs.CacheSize = 5
  335.     rs.Open cmd
  336.     if not rs.EOF then
  337.             rs.MoveFirst
  338.             rs.PageSize = 50
  339.             max_count = cInt(rs.PageCount)
  340.             num_recs = rs.RecordCount
  341.             rs.AbsolutePage = page_no
  342.             results = true
  343.     else
  344.         results = false
  345.         rs.Close
  346.     end if
  347. else
  348.     results = false
  349. end if
  350. rec_count = 0
  351.  
  352. %>
  353.  
  354. <%
  355.  
  356. if results = true then
  357.  
  358. %>
  359.  
  360. <table  >
  361. <form name="view_Cats" action="admin_cats.asp" method="get" >
  362. <tr>
  363.     <td class='fieldTD'><a href="<% =request.servervariables("script_name") %>?<% =request_string %>page_no=<% =page_no %>&sortby=CatID" class=fieldFont title="">CatID</a></td>
  364.     <td class='fieldTD'><a href="<% =request.servervariables("script_name") %>?<% =request_string %>page_no=<% =page_no %>&sortby=Cat" class=fieldFont title="">Cat</a></td>
  365.     <td class='fieldTD'><a href="<% =request.servervariables("script_name") %>?<% =request_string %>page_no=<% =page_no %>&sortby=RelatedId" class=fieldFont title="">Related To</a></td>
  366.     <td class='fieldTD'><a href="<% =request.servervariables("script_name") %>?<% =request_string %>page_no=<% =page_no %>&sortby=CatType" class=fieldFont title="">Category Type</a></td>
  367. </tr>
  368. <%
  369.  
  370. do while not rs.EOF AND (rec_count < rs.Pagesize)
  371.  
  372. ':: read db record
  373. on error resume next
  374. Parent = rs("Parent")
  375. ParentId = rs("ParentId")
  376. CatID = rs("CatID")
  377. Cat = rs("Cat")
  378. RelatedId = rs("RelatedId")
  379. CatType = rs("CatType")
  380. on error goto 0
  381.  
  382. %>
  383. <% if lastgroup <> Parent OR (isnull(lastgroup) AND Parent <> "") then %><!-- group row --><tr>    <td valign=top class=groupTD colspan=7>        <% =Parent %><br>    </td></tr><% end if %>        
  384.         <input type=hidden name="Parent" value="<% =Parent %>">
  385.         
  386.         
  387.         <input type=hidden name="ParentId" value="<% =ParentId %>">
  388.         
  389. <tr>
  390.     <td class='dataTD' >
  391.         <a href="admin_cats.asp?CatID=<%=CatID%>"><% =CatID %></a>
  392.     </td>
  393.     <td class='dataTD' >
  394.         <a href="admin_cats.asp?CatID=<%=CatID%>"><% =Cat %></a>
  395.     </td>
  396.     <td class='dataTD' >
  397.         <% =RelatedId %>
  398.     </td>
  399.     <td class='dataTD' >
  400.         <% =CatType %>
  401.     </td>
  402. </tr>
  403. <%
  404.  
  405. lastgroup = Parent
  406. rs.MoveNext
  407. rec_count = rec_count + 1
  408. loop
  409. rs.Close
  410.  
  411. %>
  412.  
  413. </form>
  414. </table>
  415. <%
  416.  
  417. else
  418.  
  419. %>
  420.  
  421.     
  422.  
  423. <%
  424.  
  425. end if
  426.  
  427. %>
  428.  
  429. <%
  430.  if max_count > 1 then 
  431. %>
  432.  
  433. <!-- paging footer -->
  434. <TABLE class=HeaderTable >
  435.     <tr>
  436.         <td width="20%" class=HeaderTD>
  437.             <% if page_no > 1 then %>
  438.             <a class=HeaderFont href="<% =request.servervariables("script_name") %>?<% =request_string %>page_no=<% =page_no-1 %>&sortby=<% =request("sortby") %>">PREV</a>
  439.             <% else %>            <% end if %>        </td>
  440.         <td align=center class=HeaderTD>
  441.             Page 
  442.             <% for i = 1 to max_count %>
  443.             <% if i = cint(page_no) then %>
  444.             <b><%=i%></b>
  445.             <% else %>
  446.             <a class=HeaderFont href="<% =request.servervariables("script_name") %>?<% =request_string %>page_no=<% =i %>&sortby=<% =request("sortby") %>"><%=i%></a>
  447.             <% end if %>
  448.             <%next %>
  449.         </td>
  450.         <td align=right width="20%" class=HeaderTD>
  451.             <% if cInt(page_no) < cInt(max_count) then %>
  452.             <a class=HeaderFont href="<% =request.servervariables("script_name") %>?<% =request_string %>page_no=<% =page_no+1 %>&sortby=<% =request("sortby") %>">NEXT</a>
  453.             <% end if %>
  454.         </td>
  455.     </tr>
  456. </TABLE>
  457. <% end if %>
  458.  
  459.  
  460.  
  461.  
  462. <!-- #include file=i_footer.asp -->
  463.  
  464.  
  465. <%
  466. ':: assure that any db resources are freed
  467. on error resume next
  468. rs.Close
  469. set rs = NOTHING
  470. cn.Close
  471. set cn = NOTHING
  472. user_cn.Close
  473. set user_cn = NOTHING
  474. on error goto 0
  475. %>
  476.